package com.buss.article.controller;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jweb.core.bean.PageMode;
import org.jweb.core.bean.ReplyDataMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.article.entity.HxArticle;
import com.buss.article.trans.form.ArticleSearchForm;
import com.buss.article.trans.vo.ArticleVO;
import com.buss.article.utils.ToolsUtil;
import com.buss.common.service.SysServiceI;

@Controller
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	private SysServiceI sysService;
	
	@RequestMapping("/list.do")
	@ResponseBody
	public ReplyDataMode articleList(ArticleSearchForm searchForm, HttpServletRequest request) {
		ReplyDataMode dataMode = new ReplyDataMode();
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();
		
		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createTime", "desc");
		orderMap.put("updateTime", "desc");
		criteriaQuery.setOrder(orderMap);
		
		List<HxArticle> articles = this.sysService.list(criteriaQuery);
		
		// 封装返回数据
		List<ArticleVO> articleVOs = new ArrayList<>();
		ArticleVO articleVO = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for(HxArticle ha : articles) {
			articleVO = new ArticleVO();
			articleVO.copyEntity(ha);
			
			if(ha.getCreateTime() != null) {
				articleVO.setCreateTime(dateFormat.format(ha.getCreateTime()));
			}
			
			if(ha.getUpdateTime() != null) {
				articleVO.setUpdateTime(dateFormat.format(ha.getUpdateTime()));
			}
			articleVOs.add(articleVO);
		}
		
		dataMode.setData(articleVOs);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);
		
		return dataMode;
	}
	
	@RequestMapping("/pagelist.do")
	@ResponseBody
	public ReplyDataMode articlePageList(ArticleSearchForm searchForm, HttpServletRequest request) throws Exception {
		ReplyDataMode dataMode = new ReplyDataMode();
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();
		
		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createTime", "desc");
		orderMap.put("updateTime", "desc");
		criteriaQuery.setOrder(orderMap);
		
		int pageNo = Integer.parseInt(searchForm.getPageNo());
		int pageSize = Integer.parseInt(searchForm.getPageSize());
		
		PageMode mode = this.sysService.list4page(criteriaQuery, pageNo, pageSize);
		List<HxArticle> articles = mode.getResult();
		
		// 封装返回数据
		List<ArticleVO> articleVOs = new ArrayList<>();
		ArticleVO articleVO = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for(HxArticle ha : articles) {
			articleVO = new ArticleVO();
			articleVO.copyEntity(ha);
			
			if(ha.getCreateTime() != null) {
				articleVO.setCreateTime(dateFormat.format(ha.getCreateTime()));
			}
			
			if(ha.getUpdateTime() != null) {
				articleVO.setUpdateTime(dateFormat.format(ha.getUpdateTime()));
			}
			articleVOs.add(articleVO);
		}
		mode.setResult(articleVOs);
		
		dataMode.setData(mode);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);
		dataMode.setAttribute(ToolsUtil.convertBean(searchForm));
		
		return dataMode;
	}
	
	
	
	
	
	
	@RequestMapping("/page.do")
	@ResponseBody
	public ReplyDataMode page(ArticleSearchForm searchForm, HttpServletRequest request) throws Exception {
		ReplyDataMode dataMode = new ReplyDataMode();
		// 搜索条件回显
		String keyword = "";
		if(searchForm.getTitle()!=null&&!"".equals(searchForm.getTitle())){			
			keyword = searchForm.getTitle();
		}
		// 手动加条件
		searchForm.setStatus("1"); // 1正常
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();
		
		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createTime", "desc");
		orderMap.put("updateTime", "desc");
		criteriaQuery.setOrder(orderMap);
		
		int pageNo = Integer.parseInt(searchForm.getPageNo());
		int pageSize = Integer.parseInt(searchForm.getPageSize());
		
		PageMode mode = this.sysService.list4page(criteriaQuery, pageNo, pageSize);
		List<HxArticle> articles = mode.getResult();
		
		// 封装返回数据
		List<ArticleVO> articleVOs = new ArrayList<>();
		ArticleVO articleVO = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for(HxArticle ha : articles) {
			articleVO = new ArticleVO();
			articleVO.copyEntity(ha);
			if(!"".equals(keyword)){			
				// 搜索 替换关键字
				articleVO.setTitle(unescapeStr(articleVO.getTitle(),keyword,"<span>"+keyword+"</span>"));
			}
			if(ha.getCreateTime() != null) {
				articleVO.setCreateTime(dateFormat.format(ha.getCreateTime()));
			}
			
			if(ha.getUpdateTime() != null) {
				articleVO.setUpdateTime(dateFormat.format(ha.getUpdateTime()));
			}
			articleVOs.add(articleVO);
		}
		mode.setResult(articleVOs);
		
		dataMode.setData(mode);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);
		Map<String, Object> attribute = ToolsUtil.convertBean(searchForm);
		attribute.put("keyword", keyword);
		dataMode.setAttribute(attribute);
		
		return dataMode;
	}
	
	public static final String unescapeStr(String str,String regex, String replacement) {
	     if (isNullValue(str))
	       return "";
	     str = str.replaceAll(regex, replacement);
	     return str;
	}
	
	public static boolean isNullValue(String value)
	{
		return (value == null) || (value.trim().equals(""));
	}
	
}
